from django.db import models
from utils.base import Base
# Create your models here.


# 创建商品表
class Goods(Base):
    PAY_TYPE = (
        ('1', '普通'),
        ('2', '促销')
    )

    # 课程外键
    course = models.ForeignKey('course.Course',on_delete=models.SET_NULL,null=True)
    # 商品价格
    price = models.IntegerField('商品价格',null=True)

    # 商品存活时间
    validity = models.IntegerField('商品有效期(单位/天)',null=True)

    pay_type = models.CharField('购买渠道', choices=PAY_TYPE, max_length=8)

    inventory = models.IntegerField('库存',null=True)

    sales_volume = models.IntegerField('销量',default=0)

    is_putaway = models.IntegerField('是否上架',default=1)

    class Meta:

        db_table = 'tb_goods'

# 已购买商品表
class UserCourse(Base):
    # 关联用户外键
    user = models.ForeignKey('user.User',on_delete=models.CASCADE,related_name='paycourse')
    # 商品外键
    goods_course = models.ForeignKey(Goods,on_delete=models.CASCADE,related_name='payuser')

    class Meta:
        db_table = 'tb_usercourse'

class Orders(Base):
    PAY_METHOD = (
        (1, "支付宝"),
    )
    ORDER_STATUS = (
        (1, "待支付"),
        (2, "已支付"),
        (3, "已取消"),
    )
    user = models.ForeignKey('user.User', on_delete=models.PROTECT, verbose_name="下单用户")
    goods = models.ForeignKey(Goods, on_delete=models.PROTECT)
    order_id = models.CharField('订单号', max_length=24)
    trade_no = models.CharField('支付宝订单号', max_length=32, null=True)  # 28位
    pay_time = models.DateTimeField('支付时间', null=True)
    pay_method = models.CharField('支付方式', choices=PAY_METHOD, default=1, max_length=8)
    status = models.CharField('支付状态', choices=ORDER_STATUS, default=1, max_length=8)
    total_amount = models.DecimalField(max_digits=10, decimal_places=2, verbose_name="商品总金额")

    class Meta:
        db_table = 'tb_orders'